﻿using System.Collections.Generic;
using Assets.Scripts.Net;
using Assets.Scripts.Net.Data;
using Assets.Scripts.Net.Data.Model;
using Assets.Scripts.Net.Loader;
using Assets.Scripts.UI.Panel;
using UnityEngine.UI;

namespace Assets.Scripts.UI.Item {
    public class PaperItem : UIBase<PaperItem> {
        public Text NumText; //试卷的数字
        public Text PaperNameText; //试卷的名称
        public Text ScoreText; //得分
        public Button DoButton; //试卷按钮
        public Button DetailButton; //详情按钮

        protected override UIType UIType {
            get { return UIType.Item; }
        }

        protected override void InitOnAwake () {
            AutoFindComponent (transform, true);
        }

        /// <summary>
        /// 试卷的信息
        /// </summary>
        /// <param name="info">信息</param>
        /// <param name="num">数字</param>
        /// <param name="isExam">是否是考试</param>
        /// <param name="checkType"></param>
        public void Init (PaperInfo info, int num, bool isExam, CheckType checkType = CheckType.Null) {
            PaperNameText.text = info.Name;
            NumText.text = num.ToString ();
            if (isExam) {
                ScoreText.gameObject.SetActive (false);
                DoButton.gameObject.SetActive (true);
                DetailButton.gameObject.SetActive (false);
                DoButton.onClick.AddListener (() => OnDoButtonClick (info));
            } else {
                ScoreText.gameObject.SetActive (true);
                ScoreText.text = "得分：" + info.Score;
                DoButton.gameObject.SetActive (false);
                //DetailButton.gameObject.SetActive(checkType != CheckType.Exam);
                DetailButton.onClick.AddListener (() => OnCheckButtonClick (info));
            }
        }

        private void OnCheckButtonClick (PaperInfo info) {
            //去掉查询成绩的缓存...可能导致不同的试卷返回结果相同
            // var queInfos =
            //     NetReaderFactory.Instance()
            //         .GetInfoByName(NetConstValue.Collect_QueInfo + "_" + info.Id) as
            //         List<QueInfo>;
            // var ansInfos = NetReaderFactory.Instance()
            //     .GetInfoByName(NetConstValue.Collect_AnsInfo + "_" + info.Id) as
            //     List<AnsInfo>;
            // if (queInfos != null && ansInfos != null)
            // {
            //     var panel = CreatePanel(PanelType.ExamPanel).GetComponent<ExamPanel>();
            //     panel.Init(ExamType.Null, info.Name, 0, queInfos, ansInfos, null, null, true, info.Score);
            //     return;
            // }
            List<QueInfo> queInfos = null;
            List<AnsInfo> ansInfos = null;
            //查询成绩
            var tsf = CreatePanel (PanelType.LoadingLoopPanel);
            tsf.GetComponent<LoadingLoopPanel> ().Init ();
            var dict = NetManager.Instance.GetDict ();
            dict.Add ("paperid", info.Id);
            dict.Add ("type", (int) CheckScorePanel._Instance.CheckType);
            NetManager.Instance.SendRequest (RequestType.GetPaperInfo, dict, () => {
                Destroy (tsf.gameObject);
                if (NetReaderFactory.Instance ().NetResUrls.Count > 0) {
                    var loadingPanel = CreatePanel (PanelType.LoadingPanel).GetComponent<LoadingPanel> ();
                    loadingPanel.Init (LoadingType.WWWLoading);
                    Global.Global.Instance.StartCoroutine (WWWLoadManager.Instance ()
                        .DownLoadSaveResource (NetReaderFactory.Instance ().NetResUrls, () => {
                            Exit ("LoadingPanel(Clone)");
                            queInfos =
                                NetReaderFactory.Instance ()
                                .GetInfoByType<List<QueInfo>> (NetConstValue.Paper_QueInfo + "_" + info.Id) as
                            List<QueInfo>;
                            ansInfos =
                                NetReaderFactory.Instance ()
                                .GetInfoByType<List<AnsInfo>> (NetConstValue.Paper_AnsInfo + "_" + info.Id) as
                            List<AnsInfo>;
                            var panel = CreatePanel (PanelType.ExamPanel).GetComponent<ExamPanel> ();
                            panel.Init (ExamType.Null, info.Name, 0, queInfos, ansInfos, null, null, true, info.Score);
                        }));
                } else {
                    var questions =
                        NetReaderFactory.Instance ()
                        .GetInfoByType<List<QueInfo>> (NetConstValue.Collect_QueInfo + "_" + info.Id) as
                    List<QueInfo>;
                    var answers = NetReaderFactory.Instance ()
                        .GetInfoByType<List<AnsInfo>> (NetConstValue.Collect_AnsInfo + "_" + info.Id) as
                    List<AnsInfo>;
                    var panel = CreatePanel (PanelType.ExamPanel).GetComponent<ExamPanel> ();
                    panel.Init (ExamType.Null, info.Name, 0, questions, answers, null, null, true, info.Score);
                }
            });
        }

        private void OnDoButtonClick (PaperInfo info) {
            //缓存中查找信息
            var queInfos =
                NetReaderFactory.Instance ().GetInfoByName (NetConstValue.Paper_QueInfo + "_" + info.Id) as
            List<QueInfo>;
            var ansInfos =
                NetReaderFactory.Instance ().GetInfoByName (NetConstValue.Paper_AnsInfo + "_" + info.Id) as
            List<AnsInfo>;
            if (queInfos != null && ansInfos != null) {
                var panel = CreatePanel (PanelType.ExamPanel).GetComponent<ExamPanel> ();
                panel.Init (ExamType.Mock, info.Name, info.Time, queInfos, ansInfos, info.Id);
                return;
            }
            //向服务器发送请求
            var tsf = CreatePanel (PanelType.LoadingLoopPanel);
            tsf.GetComponent<LoadingLoopPanel> ().Init ();
            var dict = NetManager.Instance.GetDict ();
            dict.Add ("paperid", info.Id);
            dict.Add ("type", 1);
            NetManager.Instance.SendRequest (RequestType.QuestionList, dict, () => {
                Destroy (tsf.gameObject);
                if (NetReaderFactory.Instance ().NetResUrls.Count > 0) //有需要加载的资源
                {
                    var loadingPanel = CreatePanel (PanelType.LoadingPanel).GetComponent<LoadingPanel> ();
                    loadingPanel.Init (LoadingType.WWWLoading);
                    Global.Global.Instance.StartCoroutine (WWWLoadManager.Instance ()
                        .DownLoadSaveResource (NetReaderFactory.Instance ().NetResUrls, () => {
                            Exit ("LoadingPanel(Clone)");
                            queInfos =
                                NetReaderFactory.Instance ()
                                .GetInfoByType<List<QueInfo>> (NetConstValue.Paper_QueInfo + "_" + info.Id) as
                            List<QueInfo>;
                            ansInfos =
                                NetReaderFactory.Instance ()
                                .GetInfoByType<List<AnsInfo>> (NetConstValue.Paper_AnsInfo + "_" + info.Id) as
                            List<AnsInfo>;
                            var panel = CreatePanel (PanelType.ExamPanel).GetComponent<ExamPanel> ();
                            panel.Init (ExamType.Mock, info.Name, info.Time, queInfos, ansInfos, info.Id);
                        }));
                } else {
                    queInfos =
                        NetReaderFactory.Instance ()
                        .GetInfoByType<List<QueInfo>> (NetConstValue.Paper_QueInfo + "_" + info.Id) as List<QueInfo>;
                    ansInfos =
                        NetReaderFactory.Instance ()
                        .GetInfoByType<List<AnsInfo>> (NetConstValue.Paper_AnsInfo + "_" + info.Id) as List<AnsInfo>;
                    var panel = CreatePanel (PanelType.ExamPanel).GetComponent<ExamPanel> ();
                    panel.Init (ExamType.Mock, info.Name, info.Time, queInfos, ansInfos, info.Id);
                }
            });
        }
    }
}